<template>
  <el-dialog class="login-dialog" title="编辑服务器" :visible.sync="showDialog">
    <el-form :model="client" :rules="rules" label-width="80px" label-position="right">
      <el-form-item label="IP" prop="ip">
        <el-input v-model="client.ip"/>
      </el-form-item>
      <el-form-item label="用户" prop="user">
        <el-input v-model="client.user"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="client.password"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showDialog = false">取 消</el-button>
      <el-button type="primary" @click="submit">提 交</el-button>
    </div>
  </el-dialog>
</template>

<script>

  export default {
    name: 'vpsEditor',
    data() {
      return {
        client: {ip: '', user: '', password: ''},
        rules: {
          ip: [
            {required: true, trigger: 'blur', message: '必填'},
          ],
          user: [
            {required: true, trigger: 'blur', message: '必填'},
          ],
          password: [
            {required: true, trigger: 'blur', message: '必填'},
          ],
        },
        showDialog: false,
      };
    },
    methods: {
      show(client) {
        this.client = JSON.parse(JSON.stringify(client));
        this.showDialog = true;
      },
      close() {
        this.showDialog = false;
        Object.assign(this.$data, this.$options.data());
      },
      async submit() {
        const res = await this.$http.post('/api/vps', this.client);
        if (res instanceof Error)
          return this.$alert(res.message, {type: 'error'});
        this.$message({message: '成功编辑', type: 'success'});
        this.$emit('success');
        this.close();
      },
    },
  };
</script>

<style scoped>

</style>